import Mock from 'mockjs'

// 首页轮播图
const banners = [
  { id: 1, image: require('../assets/images/banners/banner1.jpg'), url: '' },
  { id: 2, image: require('../assets/images/banners/banner2.jpg'), url: '' },
  { id: 3, image: require('../assets/images/banners/banner3.jpg'), url: '' }
]

// 分类数据
const categories = [
  {
    id: 1,
    name: '手机',
    icon: require('../assets/images/category/phone.png'),
    children: [
      {
        id: 11,
        name: '小米手机',
        image: require('../assets/images/category/mi-digital.png'),
        products: [
          {
            id: 111,
            title: '小米13 Pro',
            price: 4999,
            image: require('../assets/images/products/phones/mi13.jpg'),
            description: '徕卡专业光学镜头 | 第二代骁龙8旗舰处理器'
          },
          {
            id: 112,
            title: '小米13',
            price: 3999,
            image: require('../assets/images/products/phones/mi13-2.jpg'),
            description: '徕卡专业光学镜头 | 第二代骁龙8旗舰处理器'
          }
        ]
      },
      {
        id: 12,
        name: 'Redmi手机',
        image: require('../assets/images/category/redmi.png'),
        products: [
          {
            id: 121,
            title: 'Redmi K60',
            price: 2599,
            image: require('../assets/images/products/phones/redmi-k60.jpg'),
            description: '第二代骁龙8 | 2K 高光屏'
          }
        ]
      }
    ]
  },
  {
    id: 2,
    name: '电视',
    icon: require('../assets/images/category/tv.png'),
    children: [
      {
        id: 21,
        name: '小米电视',
        image: require('../assets/images/category/mi-tv.png'),
        products: [
          {
            id: 211,
            title: '小米电视6 65"OLED',
            price: 6999,
            image: require('../assets/images/products/tv/mi-tv-6.jpg'),
            description: '4K OLED屏幕 | 电影院级音响'
          }
        ]
      }
    ]
  },
  {
    id: 3,
    name: '笔记本',
    icon: require('../assets/images/category/laptop.png'),
    children: [
      {
        id: 31,
        name: '小米笔记本',
        image: require('../assets/images/category/mi-laptop.png'),
        products: [
          {
            id: 311,
            title: '小米笔记本Pro 14',
            price: 5599,
            image: require('../assets/images/products/laptop/mi-pro-14.jpg'),
            description: '2.8K 90Hz屏幕 | 第12代英特尔处理器'
          }
        ]
      }
    ]
  }
]

// 产品列表接口
Mock.mock('/api/products', 'get', {
  code: 0,
  data: categories.reduce((acc, category) => {
    category.children.forEach(child => {
      acc.push(...child.products)
    })
    return acc
  }, [])
})

// 分类列表接口
Mock.mock('/api/categories', 'get', {
  code: 0,
  data: [
    {
      id: 1,
      name: '手机',
      icon: require('../assets/images/category/phone.png'),
      children: [
        {
          id: 11,
          name: '小米手机',
          image: require('../assets/images/category/mi-digital.png'),
          products: [
            {
              id: 111,
              title: '小米13 Pro',
              price: 4999,
              image: require('../assets/images/products/phones/mi13.jpg'),
              description: '徕卡专业光学镜头 | 第二代骁龙8旗舰处理器'
            },
            {
              id: 112,
              title: '小米13',
              price: 3999,
              image: require('../assets/images/products/phones/mi13-2.jpg'),
              description: '徕卡专业光学镜头 | 第二代骁龙8旗舰处理器'
            }
          ]
        },
        {
          id: 12,
          name: 'Redmi手机',
          image: require('../assets/images/category/redmi.png'),
          products: [
            {
              id: 121,
              title: 'Redmi K60',
              price: 2599,
              image: require('../assets/images/products/phones/redmi-k60.jpg'),
              description: '第二代骁龙8 | 2K 高光屏'
            }
          ]
        }
      ]
    },
    {
      id: 2,
      name: '电视',
      icon: require('../assets/images/category/tv.png'),
      children: [
        {
          id: 21,
          name: '小米电视',
          image: require('../assets/images/category/mi-tv.png'),
          products: [
            {
              id: 211,
              title: '小米电视6 65"OLED',
              price: 6999,
              image: require('../assets/images/products/tv/mi-tv-6.jpg'),
              description: '4K OLED屏幕 | 电影院级音响'
            }
          ]
        }
      ]
    },
    {
      id: 3,
      name: '笔记本',
      icon: require('../assets/images/category/laptop.png'),
      children: [
        {
          id: 31,
          name: '小米笔记本',
          image: require('../assets/images/category/mi-laptop.png'),
          products: [
            {
              id: 311,
              title: '小米笔记本Pro 14',
              price: 5599,
              image: require('../assets/images/products/laptop/mi-pro-14.jpg'),
              description: '2.8K 90Hz屏幕 | 第12代英特尔处理器'
            }
          ]
        }
      ]
    }
  ]
})

// 轮播图接口
Mock.mock('/api/banners', 'get', {
  code: 0,
  data: banners
})

// 登录接口
Mock.mock('/api/login', 'post', function(options) {
  const { username, password } = JSON.parse(options.body)
  
  // 模拟的用户数据
  const users = [
    { username: 'admin', password: '123456' },
    { username: 'user', password: '123456' }
  ]
  
  const user = users.find(u => u.username === username && u.password === password)
  
  if (user) {
    return {
      'code': 0,
      'data': {
        'token': '@guid',
        'username': username
      }
    }
  } else {
    return {
      'code': 401,
      'message': '用户名或密码错误'
    }
  }
})

// 订单列表
Mock.mock('/api/orders', 'get', {
  code: 0,
  'data|10': [{
    'id|+1': 1,
    orderNo: '@guid',
    createTime: '@datetime',
    'status|1': ['待付款', '待发货', '待收货', '完成'],
    'total|1000-6000': 1000,
    products: function() {
      const allProducts = categories.reduce((acc, category) => {
        category.children.forEach(child => {
          acc.push(...child.products)
        })
        return acc
      }, [])
      const count = Mock.Random.integer(1, 3)
      const selectedProducts = Mock.Random.shuffle(allProducts).slice(0, count)
      return selectedProducts.map(product => ({
        ...product,
        quantity: Mock.Random.integer(1, 5)
      }))
    }
  }]
})

// 产品详情接口
Mock.mock(/\/api\/product\/\d+/, 'get', function(options) {
  const id = parseInt(options.url.match(/\d+/)[0])
  const allProducts = categories.reduce((acc, category) => {
    category.children.forEach(child => {
      acc.push(...child.products)
    })
    return acc
  }, [])
  const product = allProducts.find(p => p.id === id)
  
  return {
    code: 0,
    data: {
      ...product,
      specs: [
        '屏幕：2K 高清分辨率',
        '处理器：骁龙8 Gen 2',
        '电池：5000mAh',
        '快充：120W'
      ]
    }
  }
}) 